cmake_minimum_required(VERSION 2.8.0)
if(POLICY CMP0048)
   cmake_policy(SET CMP0048 OLD)
endif()

###
# Settings
#
set(PROJECT_NAME "Robo 3T" CACHE STRING "Project Name")
set(PROJECT_NAME_TITLE ${PROJECT_NAME})
set(PROJECT_DOMAIN "www.robomongo.org" CACHE STRING "Internet domain name for ${PROJECT_NAME}")
set(PROJECT_COMPANYNAME "3T Software Labs Ltd" CACHE STRING "Company name for ${PROJECT_NAME}")
set(PROJECT_COPYRIGHT "Copyright (C) 2014-2017 ${PROJECT_COMPANYNAME} All Rights Reserved.")
set(PROJECT_COMPANYNAME_DOMAIN "https://studio3t.com/" CACHE STRING "Internet domain name for ${PROJECT_NAME}")
set(PROJECT_GITHUB_FORK "www.github.com/paralect/robomongo" CACHE STRING "Git fork of ${PROJECT_NAME}")
set(PROJECT_GITHUB_ISSUES "www.github.com/paralect/robomongo/issues" CACHE STRING "Git issue link for ${PROJECT_NAME}")

string(TOLOWER ${PROJECT_NAME} PROJECT_NAME_LOWERCASE)
set(MAJOR "0")
set(MINOR "8")
set(PATCH "6")
set(BUILD "dev")
set(SHORT_VERSION ${MAJOR}.${MINOR}.${PATCH} CACHE STRING "${PROJECT_NAME} short version")
set(PROJECT_VERSION "${SHORT_VERSION}${BUILD}" CACHE STRING "${PROJECT_NAME} version")
if(APPLE)
    if(DEFINED ENV{MACOSX_DEPLOYMENT_TARGET})
        set(MACOSX_DEPLOYMENT_TARGET $ENV{MACOSX_DEPLOYMENT_TARGET})
        message("Setting MACOSX_DEPLOYMENT_TARGET to '${MACOSX_DEPLOYMENT_TARGET}'")
    elseif(EXISTS "${CMAKE_OSX_SYSROOT}")
        string(REGEX REPLACE ".*MacOSX([0-9]+)\\.([0-9]+).*$" "\\1" DARWIN_MAJOR_SDK_VERSION "${CMAKE_OSX_SYSROOT}")
        string(REGEX REPLACE ".*MacOSX([0-9]+)\\.([0-9]+).*$" "\\2" DARWIN_MINOR_SDK_VERSION "${CMAKE_OSX_SYSROOT}")
        set(MACOSX_DEPLOYMENT_TARGET ${DARWIN_MAJOR_SDK_VERSION}.${DARWIN_MINOR_SDK_VERSION})
        set(CMAKE_CXX_FLAGS "--sysroot ${CMAKE_OSX_SYSROOT} ${CMAKE_CXX_FLAGS}")
        message("Setting MACOSX_DEPLOYMENT_TARGET to '${MACOSX_DEPLOYMENT_TARGET}'")
    endif()
elseif(UNIX AND NOT APPLE)
    set(PROJECT_NAME ${PROJECT_NAME_LOWERCASE})
endif()
###
# Configure project
PROJECT(${PROJECT_NAME})

###
# Options

IF(WIN32)
    OPTION(MANIFEST_ENABLE "Enable Manifest" ON)
    if(POLICY CMP0020)
       cmake_policy(SET CMP0020 OLD)
    endif()
    if(POLICY CMP0026)
       cmake_policy(SET CMP0026 OLD)
    endif()
ENDIF(WIN32)

set(ROBOMONGO_QT_VERSION 5 CACHE STRING "Qt version to use when building Robomongo (can be 4 or 5)")
OPTION(BOOST_ENABLED "Enable boost system support" OFF)
OPTION(OPENSSL_ENABLED "Enable openssl support" ON)
OPTION(SSH_ENABLED "Enable ssh support" ON)
OPTION(CPACK_SUPPORT "Enable package support" ON)
OPTION(DEVELOPER_DYNAMIC_RUNTIME "Build with dynamic C++ runtime" ON)
OPTION(DEVELOPER_FEATURES "Use some debug tricks for developers" OFF)
OPTION(DEVELOPER_ENABLE_TESTS "Enable tests for ${PROJECT_NAME_TITLE} project" ON)

IF(MSVC)
    IF(CMAKE_CL_64)
        OPTION(BUILD_64X "Build 64bit project" ON)
    ELSE()
        OPTION(BUILD_64X "Build 64bit project" OFF)
    ENDIF()
ELSE()
    IF("${OS_ARC}" STREQUAL "64")
        OPTION(BUILD_64X "Build 64bit project" ON)
    ELSEIF("${OS_ARC}" STREQUAL "32")
        OPTION(BUILD_64X "Build 64bit project" OFF)
    ELSE()
        IF(CMAKE_SIZEOF_VOID_P MATCHES 8) 
            OPTION(BUILD_64X "Build 64bit project" ON)
        ELSE(CMAKE_SIZEOF_VOID_P MATCHES 8) 
            OPTION(BUILD_64X "Build 64bit project" OFF)
        ENDIF(CMAKE_SIZEOF_VOID_P MATCHES 8)
    ENDIF()
ENDIF()

IF(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
    IF (NOT CMAKE_BUILD_TYPE)
        SET(CMAKE_BUILD_TYPE DEBUG)
    ENDIF()
    IF(CMAKE_COMPILER_IS_GNUCXX AND NOT MINGW)
        SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
    ENDIF()
ENDIF()

INCLUDE(../cmake/config.cmake)

DEFINE_DEFAULT_DEFINITIONS()

# Search libraries
# use Qt
DETECT_QT()
##################### Libraries search ###################
IF(DEVELOPER_QT5)
    SET(QT_COMPONENTS_TO_USE Qt5Core Qt5Gui Qt5Widgets)
    IF(UNIX AND NOT APPLE)
        SET(QT_COMPONENTS_TO_USE ${QT_COMPONENTS_TO_USE} Qt5DBus) #for libqxcb
    ENDIF(UNIX AND NOT APPLE)
    IF(APPLE)
        SET(QT_COMPONENTS_TO_USE ${QT_COMPONENTS_TO_USE} Qt5MacExtras)
    ENDIF(APPLE)
ELSE()
    SET(QT_COMPONENTS_TO_USE QtCore QtGui)
ENDIF()
INTEGRATE_QT(${QT_COMPONENTS_TO_USE})
##########################################################

IF(BUILD_64X)
    SET_PROPERTY(GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS 1)
    IF(CMAKE_COMPILER_IS_GNUCXX)
        SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m64")
        SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64 -std=gnu++0x -Wno-unused-local-typedefs -Wno-unused-parameter -Wno-literal-suffix -Wdeprecated-declarations")
        SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m64")
        SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m64")
        SET(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -m64")
    ELSEIF(MSVC)
        SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /machine:x64")
        SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /machine:x64")
        SET(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /machine:x64")
    ENDIF()
    IF(APPLE)
        SET(CMAKE_OSX_ARCHITECTURES x86_64)
    ENDIF()
    SET(ARCH_TEXT 64)
    SET(ARCH_TEXT_PACKAGE x86_64)#amd64
ELSE()
    SET_PROPERTY(GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS 0)
    IF(CMAKE_COMPILER_IS_GNUCXX)
        SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32")
        SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
        SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32")
        SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32")
        SET(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -m32")
    ELSEIF(MSVC)
        SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /machine:x86")
        SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /machine:x86")
        SET(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /machine:x86")
    ENDIF()
    IF(APPLE)
        SET(CMAKE_OSX_ARCHITECTURES i386)
        #-DCMAKE_OSX_ARCHITECTURES= ;i386
    ENDIF()
    SET(ARCH_TEXT 32)
    SET(ARCH_TEXT_PACKAGE i386)
ENDIF()

##################################DEFAULT VALUES##########################################
IF(NOT CMAKE_INSTALL_PREFIX)
    SET(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install")
ENDIF()

IF(".." STREQUAL "${PROJECT_BINARY_DIR}")
    MESSAGE(SEND_ERROR "In-source builds are not allowed.")
ENDIF(".." STREQUAL "${PROJECT_BINARY_DIR}")

############################################################################
IF(WIN32)
    SET(RESOURCE_OS    "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/win.rc")
    SET(RESOURCE_OS_IN "${CMAKE_SOURCE_DIR}/install/windows/winres.rc.in")
    SET(ICON_FILE      "${CMAKE_SOURCE_DIR}/src/robomongo/gui/resources/robo.ico")
    SET(LIB_INSTALL_DESTINATION ..)
    SET(TARGET_INSTALL_DESTINATION ..)
ELSEIF(APPLE)
    SET(RESOURCE_OS "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/robomongo.desktop")
    SET(RESOURCE_OS_IN "${CMAKE_SOURCE_DIR}/install/robomongo.desktop.in")
    SET(ICON_FILE "${CMAKE_SOURCE_DIR}/install/osx/Robomongo.icns")
    SET(LIB_INSTALL_DESTINATION ..)
    SET(TARGET_INSTALL_DESTINATION ..)
ELSEIF(UNIX)
    SET(RESOURCE_OS "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}.desktop")
    SET(RESOURCE_OS_IN "${CMAKE_SOURCE_DIR}/install/robomongo.desktop.in")
    SET(ICON_FILE      "${CMAKE_SOURCE_DIR}/install/linux/robomongo.png")
    SET(LIB_INSTALL_DESTINATION lib)
    SET(TARGET_INSTALL_DESTINATION bin)
ENDIF()
# project sources
SET_PROPERTY( GLOBAL PROPERTY USE_FOLDERS ON)

ADD_SUBDIRECTORY(../src)
IF(CPACK_SUPPORT)
    SET(CPACK_PACKAGE_DESCRIPTION "Shell-centric cross-platform MongoDB management tool.")
    SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "${PROJECT_NAME_TITLE} shell-centric cross-platform MongoDB management tool.")#CPACK_DEBIAN_PACKAGE_DESCRIPTION CPACK_RPM_PACKAGE_SUMMARY
    SET(CPACK_PACKAGE_DESCRIPTION_FILE "../DESCRIPTION")#CPACK_RPM_PACKAGE_DESCRIPTION
    SET(CPACK_RESOURCE_FILE_LICENSE "../LICENSE")
    SET(CPACK_RESOURCE_FILE_README "../README.md")
    SET(CPACK_RESOURCE_FILE_WELCOME "../README.md")

    SET(CPACK_PACKAGE_VENDOR ${PROJECT_COMPANYNAME})#CPACK_RPM_PACKAGE_VENDOR
    SET(CPACK_PACKAGE_CONTACT "robo3t@3t.io")#CPACK_DEBIAN_PACKAGE_MAINTAINER
    
    SET(CPACK_PACKAGE_VERSION_MAJOR ${MAJOR})
    SET(CPACK_PACKAGE_VERSION_MINOR ${MINOR})
    SET(CPACK_PACKAGE_VERSION_PATCH ${SHORT_VERSION})
    IF(BUILD)
        STRING(REGEX REPLACE " " "" BUILD_ESCAPE ${BUILD})
        SET(CPACK_PACKAGE_VERSION_PATCH "${CPACK_PACKAGE_VERSION_PATCH}-${BUILD_ESCAPE}")
    ENDIF(BUILD)
    SET(CPACK_PACKAGE_VERSION ${CPACK_PACKAGE_VERSION_PATCH})#CPACK_DEBIAN_PACKAGE_VERSION CPACK_RPM_PACKAGE_VERSION
    SET(CPACK_PACKAGE_NAME ${PROJECT_NAME})#CPACK_DEBIAN_PACKAGE_NAME CPACK_RPM_PACKAGE_NAME
    SET(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${ARCH_TEXT_PACKAGE}")#out package name
    SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}")  
    SET(CPACK_PACKAGE_INSTALL_DIRECTORY ${PROJECT_NAME})
    SET(CPACK_PACKAGE_EXECUTABLES "${PROJECT_NAME_TITLE};${PROJECT_NAME}")
    MESSAGE(STATUS "CPACK_PACKAGE_FILE_NAME: ${CPACK_PACKAGE_FILE_NAME}") 
    SET(CPACK_MONOLITHIC_INSTALL ON)

    IF(WIN32)
        IF(NOT CPACK_GENERATOR)
            SET(CPACK_GENERATOR NSIS)
        ENDIF(NOT CPACK_GENERATOR)
        IF(CPACK_GENERATOR STREQUAL "NSIS")
            # There is a bug in NSI that does not handle full unix paths properly. Make
            # sure there is at least one set of four (4) backlasshes.
            #SET(CPACK_NSIS_MODIFY_PATH ON)
            SET(CPACK_PACKAGE_ICON "${CMAKE_SOURCE_DIR}/install/windows\\\\nsis-top-logo.bmp")
            SET(CPACK_NSIS_INSTALLED_ICON_NAME "\\\\${PROJECT_NAME}.exe")            
            SET(CPACK_CREATE_DESKTOP_LINKS "${PROJECT_NAME}.exe")
            SET(CPACK_NSIS_CREATE_ICONS "CreateShortCut \\\"$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\${PROJECT_NAME}.lnk\\\" \\\"$INSTDIR\\\\${PROJECT_NAME}.exe\\\"")
            SET(CPACK_NSIS_CREATE_ICONS_EXTRA "CreateShortCut  \\\"$DESKTOP\\\\${PROJECT_NAME}.lnk\\\" \\\"$INSTDIR\\\\${PROJECT_NAME}.exe\\\"")
            SET(CPACK_NSIS_DELETE_ICONS_EXTRA "Delete           \\\"$DESKTOP\\\\${PROJECT_NAME}.lnk\\\"")
            SET(CPACK_NSIS_DISPLAY_NAME "${CPACK_PACKAGE_INSTALL_DIRECTORY}")
            SET(CPACK_NSIS_HELP_LINK "http:\\\\\\\\${PROJECT_COMPANYNAME_DOMAIN}")
            SET(CPACK_NSIS_URL_INFO_ABOUT "http:\\\\\\\\${PROJECT_DOMAIN}")
            SET(SIDEBAR_IMAGE ${CMAKE_SOURCE_DIR}/install/windows\\\\database.bmp)
            SET(CPACK_NSIS_INSTALLER_MUI_ICON_CODE "!define MUI_WELCOMEFINISHPAGE_BITMAP \\\"${SIDEBAR_IMAGE}\\\"")
            SET(CPACK_NSIS_CONTACT "me@my-personal-home-page.com")            
            SET(CPACK_NSIS_MUI_FINISHPAGE_RUN "..\\\\${PROJECT_NAME}.exe")
        ENDIF(CPACK_GENERATOR STREQUAL "NSIS")
    ELSEIF(APPLE)
        IF(NOT CPACK_GENERATOR)
            SET(CPACK_GENERATOR DragNDrop)
        ENDIF(NOT CPACK_GENERATOR)
        #SET(CPACK_OSX_PACKAGE_VERSION "10.5")
    ELSEIF(UNIX)
        SET(CPACK_STRIP_FILES ON)
        #SET(UBUNTU_LP_BUG 300472)
        #SET(CPACK_STRIP_FILES "bin/${PROJECT_NAME}")
        #SET(CPACK_SOURCE_STRIP_FILES "")
        IF(NOT CPACK_GENERATOR)
            SET(CPACK_GENERATOR DEB)
        ENDIF(NOT CPACK_GENERATOR)
        IF(CPACK_GENERATOR STREQUAL "RPM" OR CPACK_GENERATOR STREQUAL "DEB")
            SET(CPACK_PACKAGING_INSTALL_PREFIX "/opt/${PROJECT_NAME}")
        ENDIF(CPACK_GENERATOR STREQUAL "RPM" OR CPACK_GENERATOR STREQUAL "DEB")
        CONFIGURE_FILE("../install/linux/postinst.in" "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/scripts/postinst" @ONLY IMMEDIATE)
        FILE(COPY ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/scripts/postinst DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME} 
            FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
        CONFIGURE_FILE("../install/linux/prerm.in" "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/scripts/prerm" @ONLY IMMEDIATE)
        FILE(COPY ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/scripts/prerm DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME} 
            FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
        IF(CPACK_GENERATOR STREQUAL "RPM")
            #CPACK_RPM_PACKAGE_ARCHITECTURE CPACK_RPM_PACKAGE_LICENSE CPACK_RPM_PACKAGE_DESCRIPTION CPACK_RPM_SPEC_INSTALL_POST
            #SET(CPACK_PACKAGE_RELEASE 1)
            SET(CPACK_RPM_PACKAGE_LICENSE "GPL v3")
            #SET(CPACK_RPM_PACKAGE_AUTOREQPROV "yes")
            SET(CPACK_RPM_PACKAGE_REQUIRES "libc.so.6 >= 2.12, libstdc++.so.6 >= 6.0.13, glibc, openssl, libgcc, mesa-libGL, libicu, gtk2, dbus, pango, libSM, libICE, liberation-sans-fonts")
            SET(CPACK_RPM_PACKAGE_RELEASE 1)
            SET(CPACK_RPM_PACKAGE_GROUP "Development/Libraries/${PROJECT_COMPANYNAME}")
            SET(CPACK_RPM_PACKAGE_ARCHITECTURE ${ARCH_TEXT_PACKAGE})
            SET(CPACK_RPM_PACKAGE_VERSION ${SHORT_VERSION})
            SET(CPACK_RPM_POST_INSTALL_SCRIPT_FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/postinst")
            SET(CPACK_RPM_PRE_UNINSTALL_SCRIPT_FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/prerm")
        ELSEIF(CPACK_GENERATOR STREQUAL "DEB")
            CONFIGURE_FILE("../install/linux/fixup_deb.sh.in" "${CMAKE_CURRENT_BINARY_DIR}/fixup_deb.sh" @ONLY IMMEDIATE)
            IF(ARCH_TEXT_PACKAGE STREQUAL x86_64)
                SET(ARCH_TEXT_PACKAGE amd64)
            ENDIF()
            SET(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
            #CPACK_DEBIAN_PACKAGE_HOMEPAGE The URL of the web site for this package
            SET(CPACK_DEBIAN_PACKAGE_DEBUG ON)
            SET(CPACK_DEBIAN_PACKAGE_PRIORITY "optional")
            SET(CPACK_DEBIAN_PACKAGE_SECTION "database")#input our section
            SET(CPACK_DEBIAN_PACKAGE_RECOMMENDS "Unknown")
            SET(CPACK_DEBIAN_PACKAGE_SUGGESTS "Unknown")
            SET(CPACK_DEBIAN_PACKAGE_ARCHITECTURE ${ARCH_TEXT_PACKAGE})#i386
            SET(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/postinst;${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/prerm")
        ENDIF(CPACK_GENERATOR STREQUAL "RPM")
    ENDIF(WIN32)
    INCLUDE(CPack)
ENDIF(CPACK_SUPPORT)
